<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container" :style="{'min-height': containerHeight + 'px'}">
            <div class></div>
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-folder-add" @click="goAdd">新增</el-button>
                <el-button type="warning" icon="el-icon-edit" @click="goEdit">修改</el-button>
                <el-button type="danger" icon="el-icon-delete" @click="delAllSelection">删除</el-button>

                <el-form :model="query" ref="searchRuleForm" label-width="100px">
                    <el-row>
                        <el-col :sm="20" :md="5">
                            <el-form-item label="姓名:" prop="name">
                                <el-input v-model="query.name" placeholder="请输入"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="5">
                            <el-form-item label="状态:" prop="onbordingStatus">
                                <el-select
                                    v-model="query.onbordingStatus"
                                    placeholder="请选择"
                                    class="handle-select mr10"
                                >
                                    <el-option key="1" label="在职" value="0"></el-option>
                                    <el-option key="2" label="离职" value="1"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="9">
                            <el-form-item label="入职时间:">
                                <el-date-picker
                                    v-model="onbordingTime"
                                    type="daterange"
                                    range-separator="至"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                                    style="width: 80%"
                                    @change="onBordingChange"
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :sm="20" :md="5">
                            <el-form-item label="部门:" prop="departName">
                                <el-input v-model="query.departName" placeholder="请输入"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="5">
                            <el-form-item label="岗位:" prop="jobRole">
                                <el-input v-model="query.jobRole" placeholder="请输入"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="9">
                            <el-form-item label="生日时间:">
                                <el-date-picker
                                    v-model="birthdayTime"
                                    type="daterange"
                                    range-separator="至"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                                    style="width: 80%"
                                    @change="birthChange"
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="4">
                            <el-button
                                type="primary"
                                class
                                icon="el-icon-search"
                                @click="handleSearch"
                            >搜索</el-button>
                            <el-button type="info" @click="resetForm('searchRuleForm')">重置</el-button>
                        </el-col>
                    </el-row>
                </el-form>
            </div>

            <div class="tree-box flex">
                <div class="flex-left">
                    <el-tree
                        :data="treeData"
                        :props="defaultProps"
                        @node-click="handleNodeClick"
                        default-expand-all
                        :expand-on-click-node="false"
                    ></el-tree>
                </div>
                <div class="flex-right">
                    <!-- 表格 -->
                    <table-List
                        ref="tableList"
                        :tableData="tableData"
                        :tableHeader="tableHeader"
                        :loading="loading"
                        :pagination="pagination"
                        @pageChange="pageChange"
                        @selectFun="selectFun"
                        @sortChange="sortChange"
                    ></table-List>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import appMain from '../../../utils/app_main';
export default {
    name: 'employeeList',
    data() {
        return {
            containerHeight: '',
            query: {
                birthdayEndTime: '', //生日查询结束时间
                birthdayStartTime: '', //生日查询开始时间
                departName: '', //部门名字
                departId: '', //部门编号
                jobRole: '', //所属岗位
                jobRoleId: '', //所属岗位ID
                name: '', //姓名
                onbordingEndTime: '', //入职查询结束时间
                onbordingStartTime: '', //入职查询开始时间
                onbordingStatus: '', //入职状态,在职/离职
                sortColumn: '', //排序字段 倒序追加 columnName desc
                userId: '' //用户ID
            },
            //入职时间
            onbordingTime: [],
            birthdayTime: [],
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            treeData: [],
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            showEditDialog: true,
            pageTitle: '',
            loading: false,
            // 分页信息
            pagination: {
                pageIndex: 1,
                pageSize: 10,
                pageTotal: 60
            },
            tableHeader: [
                // 表头数据
                { prop: 'id', label: '人员编号', minWidth: '100px', router: 'employeeDetail', routerId: 'id', sortable: 'custom' },
                { prop: 'departName', label: '部门', minWidth: '100px', sortable: 'custom' },
                {
                    prop: 'name',
                    label: '姓名',
                    router: 'employeeDetail',
                    routerId: 'id',
                    sortable: 'custom'
                },
                { prop: 'jobNumber', label: '工号', minWidth: '100px', sortable: 'custom' },
                {
                    prop: 'gender',
                    label: '性别',
                    sortable: 'custom',
                    formatData: function(val) {
                        return val == true ? '男' : '女';
                    }
                },
                {
                    prop: 'birthday',
                    label: '生日',
                    formatData: function(val) {
                        let date = new Date(val);
                        return appMain.formatDate(date, 'yyyy-MM-dd');
                    },
                    sortable: 'custom'
                },

                { prop: 'jobRole', label: '岗位', minWidth: '100px', sortable: 'custom' },
                { prop: 'itileLevelName', label: '职称职等', minWidth: '100px', sortable: 'custom' },
                {
                    prop: 'onbordingTime',
                    label: '入职时间',
                    formatData: function(val) {
                        let date = new Date(val);
                        return appMain.formatDate(date, 'yyyy-MM-dd');
                    },
                    sortable: 'custom'
                }
            ]
        };
    },
    created() {
        if (this.$route.query.id) {
            this.query.departId = this.$route.query.id;
        }
        this.getData();
        this.getTree();
    },

    methods: {
        //分页导航
        pageChange(data) {
            this.pagination = data.backData;
            console.log('pageChange:', this.pagination);
            //分页变化--请求接口
            this.getData();
        },
        // 组件选择完后把数据传过来
        selectFun(data) {
            this.multipleSelection = data.backData;
        },
        //表格组件返回排序对象
        sortChange(data) {
            let column = data.backData;
            //排序
            if (column.order) {
                //倒序
                if (column.order === 'descending') {
                    this.query.sortColumn = column.prop + ' ' + 'desc';
                } else {
                    this.query.sortColumn = column.prop;
                }
            } else {
                //不排序
                this.query.sortColumn = '';
            }
            this.getData();
        },
        //入职时间
        onBordingChange(val) {
            console.log('onBordingChange:', val);
            if (val == null) {
                this.onbordingTime = [];
                this.query.onbordingStartTime = '';
                this.query.onbordingEndTime = '';
                return;
            }
            this.onbordingTime = val;
            this.query.onbordingStartTime = val[0];
            this.query.onbordingEndTime = val[1];
        },
        birthChange(val) {
            console.log('birthChange:', val);
            if (val == null) {
                this.birthdayTime = [];
                this.query.birthdayStartTime = '';
                this.query.birthdayEndTime = '';
                return;
            }
            this.birthdayTime = val;
            this.query.birthdayStartTime = val[0];
            this.query.birthdayEndTime = val[1];
        },
        //搜索
        getData() {
            this.loading = true;
            let params = {
                pageNo: this.pagination.pageIndex,
                pageSize: this.pagination.pageSize,
                record: this.query
            };
            appMain
                .employeeFindAllPageByDTO(params)
                .then(res => {
                    if (res.data.code === 20000) {
                        //TODO 测试环境使用
                        this.timer = setTimeout(() => {
                            //设置延迟执行
                            this.loading = false;
                        }, 1000);
                        this.tableData = res.data.data.list;
                        this.pagination.pageSize = res.data.data.pageSize;
                        this.pagination.pageIndex = res.data.data.pageNum;
                        this.pagination.pageTotal = res.data.data.total;
                    } else {
                        this.loading = false;
                        this.$message.error('服务器繁忙，请稍后再试');
                    }
                })
                .catch(err => {
                    this.loading = false;
                    console.log(err);
                });
        },
        // 触发搜索按钮
        handleSearch() {
            //this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },

        delAllSelection() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //请求接口删除数据
                    this.deleteById(this.multipleSelection[0].id);
                })
                .catch(() => {});
        },
        deleteById(id) {
            appMain
                .delEmployee({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.getData();
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },

        // 获取树的数据
        getTree() {
            appMain
                .organizationList()
                .then(res => {
                    if (res.data.code === 20000) {
                        this.treeData = res.data.data;
                    } else {
                        this.$message.error('服务器繁忙，请稍后再试');
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        // 树元素点击事件
        handleNodeClick(e) {
            console.log(e, 'treeItem');
            // 发请求获取右下表格数据
            this.query.departId = e.id;
            this.query.departName = e.name;
            this.getData();
        },

        goAdd() {
            this.$router.push({
                path: '/employee'
            });
        },
        goEdit() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            this.$router.push({
                path: '/employee',
                query: {
                    id: this.multipleSelection[0].id
                }
            });
        },
        goDetail(id) {
            this.$router.push({
                path: '/employeeDetail',
                query: {
                    id: 45665
                }
            });
        },
        resetForm(formName) {
            if (this.tableData.length > 0) {
                this.$refs.tableList.clearSort();
            }
            console.log('resetForm');
            this.$refs[formName].resetFields();
            this.query.birthdayEndTime = '';
            this.query.birthdayStartTime = '';
            this.query.onbordingEndTime = '';
            this.query.onbordingStartTime = '';
            this.query.departName = '';
            this.query.sortColumn = '';
            this.query.departId = '';
            this.query.jobRole = '';
            this.query.jobRoleId = '';
            this.query.name = '';
            this.query.userId = '';
            this.onbordingTime = [];
            this.birthdayTime = [];
        }
    },
    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 160;
        this.pageTitle = this.$router.currentRoute.meta.title;
    },
    watch: {
        'query.departName'() {
            if (this.query.departName == '') {
                this.query.departId = '';
            }
        }
    }
};
</script>

<style scoped>
.container {
    box-sizing: border-box;
}
.handle-box {
    margin-bottom: 20px;
    flex-wrap: wrap;
    font-size: 12px;
}
.handle-box > div,
.handle-box > button {
    margin-bottom: 15px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 120px;
    display: inline-block;
}
.tree-box {
    border: 1px solid #ddd;
    margin-top: 20px;
    height: calc(100% - 146px);
}
.tree-box .flex-left {
    width: 200px;
    border-right: 1px solid #ddd;
}
.tree-box .flex-right {
    width: calc(100% - 200px);
}
.table-box {
    height: calc(100% - 72px);
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.ml10 {
    margin-left: 10px;
}
.el-input {
    max-width: 220px;
}
.el-date-editor {
    width: 100% !important;
}
</style>
